package com.xiu.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xiu.pojo.ExamGrade;
import com.xiu.pojo.ExamSubject;
import com.xiu.pojo.ExamTi;
import com.xiu.service.SubjectManageService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping(value="/exam")
public class TeacherController {
	
	@Autowired
	SubjectManageService subjectManageService;
	
	ExamSubject examSubject = new ExamSubject();
	
	//显示所有科目信息
	@RequestMapping(value="/subject",method=RequestMethod.GET)
	public String subjectManage(Model model){
		List<ExamSubject> subjectList = subjectManageService.findAllSubjects();
		model.addAttribute("subjectList", subjectList);
		model.addAttribute("examSubject", examSubject);
		return "exam/subjectsMain";
	}
	
	//增加科目
	@RequestMapping(value="/addsubject",method=RequestMethod.POST)
	public void addSubjects(@ModelAttribute ExamSubject examSubject,Model model,RedirectAttributes re,HttpServletResponse response){
		int count = subjectManageService.addSubjects(examSubject);
		JSONObject json = new JSONObject();
		try {
			if(count == 1){
				List<ExamSubject> subjectList = subjectManageService.findAllSubjects();
				model.addAttribute("subjectList", subjectList);
				re.addFlashAttribute("addMsg", "增加成功");
				json.put("result", "success");
			}else{
				json.put("result", "error");
			}
			PrintWriter pw = response.getWriter();
			pw.write(json.toString());
			pw.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	//删除科目
	@RequestMapping(value="deletesubject/{examSubjectId}",method=RequestMethod.GET)
	public String deleteSubject(Model model,@PathVariable int examSubjectId,RedirectAttributes re){
		int count = subjectManageService.deleteSubjects(examSubjectId);
		if(count == 1){
			List<ExamSubject> subjectList = subjectManageService.findAllSubjects();
			model.addAttribute("subjectList", subjectList);
			re.addFlashAttribute("msg", "删除成功");
		}else{
			List<ExamSubject> subjectList = subjectManageService.findAllSubjects();
			model.addAttribute("subjectList", subjectList);
			re.addFlashAttribute("msg", "删除失败");
			return "exam/subjectsMain";
		}
		return "redirect:/exam/subject";
	}

	
	//修改1科目：跳转静态模态框
	@RequestMapping(value="/editsubject/{id}",method = RequestMethod.GET)
	public void edit(@PathVariable int id,Model model,HttpServletResponse response)throws IOException, JSONException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		examSubject = subjectManageService.findSubjectById(id);
		JSONObject json = new JSONObject();
		JSONArray array= JSONArray.fromObject(examSubject);
		json.put("subjects", array);
		try {
			response.getWriter().write(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	
	
	//修改2科目：静态模态框修改
	@RequestMapping(value="/editsubject",method = RequestMethod.POST)
	public void edit(@Valid @ModelAttribute ExamSubject examSubject,Model model,HttpServletResponse response)throws IOException{
		//examUser.setExamBirthday(DateUtil.stringToDate(examUser.getExamBirthdayString()));
		int count = subjectManageService.updateSubjects(examSubject);
		JSONObject json = new JSONObject();
		try {
			if(count == 1){
				List<ExamSubject> subjectList = subjectManageService.findAllSubjects();
				model.addAttribute("subjectList", subjectList);
				json.put("result", "success");
			}else{
				json.put("result", "error");
				//re.addFlashAttribute("addMsg", "增加失败");
				}
			}catch (JSONException e) {
				e.printStackTrace();
				}
			PrintWriter pw = response.getWriter();
			pw.write(json.toString());
			pw.close();
		}
	
	
	//通过科目id查找成绩
	@RequestMapping(value="/searchGradeById/{examSubjectId}",method=RequestMethod.GET)
	public String searchGradeById(@PathVariable int examSubjectId,Model model,ExamGrade examGrade,HttpServletResponse response){
		List<ExamGrade> gradeList = subjectManageService.findGradeById(examSubjectId);
		if(examGrade != null){
			model.addAttribute("gradeList", gradeList);
		}else{
			return "exam/subjectsMain";
		}
		return "exam/gradeMain";
	}
	
	//查询所有成绩
	@RequestMapping(value="/gradeList",method=RequestMethod.GET)
	public String findAllGrade(Model model,ExamGrade examGrade){
		List<ExamGrade> gradeList = subjectManageService.findAllGrade();
		//examGrade.setExamGradeTime(DateUtil.stringToDate(examGrade.getExamStringGradeTime()));
		model.addAttribute("gradeList", gradeList);
		model.addAttribute("examGrade", examGrade);
		return "exam/gradeMain";
	}
	
	//通过4个条件查询成绩
	@RequestMapping(value="/searchGradeBy4",method=RequestMethod.POST)
	public String searchGrade4(ExamGrade examGrade,Model model){
		List<ExamGrade> gradeList = subjectManageService.searchBy(examGrade);
		model.addAttribute("gradeList", gradeList);
		return "exam/gradeMain";
	}
	
	//显示所有的考试题目
	@RequestMapping(value="/questionList",method=RequestMethod.GET)
	public String question(Model model,ExamTi examTi){
		List<ExamTi> tiList = subjectManageService.findAllTi();
		model.addAttribute("tiList", tiList);
		model.addAttribute("examTi", examTi);
		return "exam/questionMain";
	}
	
	//增加考试题目
	@RequestMapping(value="/addTi",method=RequestMethod.POST)
	public void addTi(@ModelAttribute ExamTi examTi,Model model,RedirectAttributes re,HttpServletResponse response){
		int count = subjectManageService.addTi(examTi);
		JSONObject json = new JSONObject();
		try {
			if(count == 1){
				List<ExamTi> tiList = subjectManageService.findAllTi();
				model.addAttribute("tiList", tiList);
				re.addFlashAttribute("addMsg", "增加成功");
				json.put("result", "success");
			}else{
				json.put("result", "error");
			}
			PrintWriter pw = response.getWriter();
			pw.write(json.toString());
			pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//删除考题
	@RequestMapping(value="deleteTi/{examTiId}",method=RequestMethod.GET)
	public String deleteTi(Model model,@PathVariable int examTiId,RedirectAttributes re){
		int count = subjectManageService.deleteTi(examTiId);
		if(count == 1){
			List<ExamTi> tiList = subjectManageService.findAllTi();
			model.addAttribute("tiList", tiList);
			re.addFlashAttribute("msg", "删除成功");
		}else{
			List<ExamTi> tiList = subjectManageService.findAllTi();
			model.addAttribute("tiList", tiList);
			re.addFlashAttribute("msg", "删除失败");
			return "exam/questionMain";
		}
		return "redirect:/exam/questionList";
	}

	//修改1考题：跳转静态模态框
	@RequestMapping(value="/editTi/{id}",method = RequestMethod.GET)
	public void editTi(@PathVariable int id,Model model,ExamTi examTi,HttpServletResponse response)throws IOException, JSONException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		examTi = subjectManageService.findTiById(id);
		JSONObject json = new JSONObject();
		JSONArray array= JSONArray.fromObject(examTi);
		json.put("tis", array);
		try {
			response.getWriter().write(json.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	//修改2科目：静态模态框修改
	@RequestMapping(value="/editTi",method = RequestMethod.POST)
	public void editTi(@Valid @ModelAttribute ExamTi examTi,Model model,HttpServletResponse response)throws IOException{
		//examUser.setExamBirthday(DateUtil.stringToDate(examUser.getExamBirthdayString()));
		int count = subjectManageService.updateTi(examTi);
		JSONObject json = new JSONObject();
		try {
			if(count == 1){
				List<ExamTi> tiList = subjectManageService.findAllTi();
				model.addAttribute("tiList", tiList);
				json.put("result", "success");
			}else{
				json.put("result", "error");
				}
			}catch (JSONException e) {
				e.printStackTrace();
				}
			PrintWriter pw = response.getWriter();
			pw.write(json.toString());
			pw.close();
		}

}
